package project.work;

import project.controllers.ConsoleScanner;
import project.controllers.Messager;
import project.controllers.Printer;
import project.menu.Builder;
import project.menu.IMenu;
import project.menu.IMenuItem;
import project.menu.mainmenu.MainMenu;
import controllers.Autoservice;
import controllers.IAutoservice;

public class Navigator {

	public static  Navigator NAVIGATOR;
	private Integer choice;
	private IMenu currentMenu;
	private IMenuItem currentMenuItem;
	private IAutoservice aut=new Autoservice();
	private MainMenu mm;
	private ConsoleScanner cs;
	
	
	private Navigator(){
		mm=MainMenu.getInstance();
		cs=ConsoleScanner.getInstance();		
		
	}
	
	public static Navigator getInstance(){
		if(NAVIGATOR == null){
			NAVIGATOR  = new Navigator();
		}
		return NAVIGATOR;
	}


	public void navigation() {
		Builder.build();
		Printer.printMenu(mm.getMenu());
		currentMenu = mm;
		
		Printer.printMessage(Messager.MAKE_CHOICE);

		do {
			try{
				choice = cs.getChoice();
				if (choice > (currentMenu.getMenu().size() - 1)) {
					Printer.printMessage(Messager.ERROR_CHOICE);
				}
	
				for (IMenuItem i : getCurrentMenu().getMenu()) {
					if (choice == i.getId()) {
						currentMenuItem = i;
						i.action(aut);
					}
				}
			} catch (Exception e) {
				System.out.println(Messager.ERROR_CHOICE);
			}

		} while (currentMenuItem != mm.getMenu().get(4));
		Printer.printMessage(Messager.THANKS);
		

	}

	public void drawMenu(IMenu menu) {
		currentMenu = menu;
		Printer.printMessage(Messager.MAKE_CHOICE);
		Printer.printMenu(currentMenu.getMenu());
	}

	private IMenu getCurrentMenu() {
		return currentMenu;
	}

}